package com.hdoit.ares.web.home.controllers;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.hdoit.ares.web.cbase.AppWechatPayService;
import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.logical.wechat.model.WechatOrder;
import com.tennetcn.common.logical.wechat.service.IWechatOrderService;
import com.tennetcn.common.selfcommon.enums.wechat.WechatOrderStatus;
import com.tennetcn.component.intceptor.IWechatPayIntceptor;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.web.cbase.AuthPassport;


@Controller
@RequestMapping("/appWechatPay/")
@AuthPassport
public class AppWechatController extends BaseController{

	private AppWechatPayService wechatPayService;
	private IWechatOrderService wechatOrderService;
	
	public IWechatOrderService getWechatOrderService() {
		return wechatOrderService;
	}

	@Resource
	public void setWechatOrderService(IWechatOrderService wechatOrderService) {
		this.wechatOrderService = wechatOrderService;
	}
	
	public AppWechatPayService getWechatPayService() {
		return wechatPayService;
	}

	@Resource
	public void setWechatPayService(AppWechatPayService wechatPayService) {
		this.wechatPayService = wechatPayService;
	}
	
	
	private final String payViewName="appPay";
	
	
	@ResponseBody
	@RequestMapping(value=payViewName,produces = "text/plain;charset=utf-8",method={RequestMethod.GET,RequestMethod.POST})
	public String pay(@RequestBody String requestBody) {
		System.out.println("回调");
		try {
			WxPayOrderNotifyResult payOrderNotifyResult=wechatPayService.parseOrderNotifyResult(requestBody);
			payOrderNotifyResult.checkResult(wechatPayService);
			//如果到这里就说明验证签名是通过的
			
			WechatOrder wechatOrder=fillWechatOrder(payOrderNotifyResult);
			
			//子级项目监听
			IWechatPayIntceptor wechatPayIntceptor=getWechatPayIntceptor();
			if(wechatPayIntceptor!=null){
				wechatPayIntceptor.pay(payOrderNotifyResult,wechatOrder);
			}
		} catch (WxPayException e) {
			e.printStackTrace();
			return WxPayNotifyResponse.fail(e.getMessage());
		}
		return WxPayNotifyResponse.success("OK");
	}

	private WechatOrder fillWechatOrder(WxPayOrderNotifyResult payOrderNotifyResult){
		String orderId=payOrderNotifyResult.getOutTradeNo();
		
		WechatOrder wechatOrder=wechatOrderService.queryModel(orderId);
		wechatOrder.setTimeEnd(payOrderNotifyResult.getTimeEnd());
		wechatOrder.setTransactionId(payOrderNotifyResult.getTransactionId());
		wechatOrder.setBankType(payOrderNotifyResult.getBankType());
		wechatOrder.setFeeType(payOrderNotifyResult.getFeeType());
		wechatOrder.setDeviceInfo(payOrderNotifyResult.getDeviceInfo());
		if("success".equals(payOrderNotifyResult.getReturnCode().toLowerCase())){
			wechatOrder.setOrderStatus(WechatOrderStatus.paySuccess);
		}else{
			wechatOrder.setOrderStatus(WechatOrderStatus.payFail);
		}
		
		if(wechatOrderService.updateModel(wechatOrder)){
			return wechatOrder;
		}
		return null;
	}
	
	private IWechatPayIntceptor getWechatPayIntceptor(){
		IWechatPayIntceptor wechatPayIntceptor = CommonApplicationContextUtil.getCurrentContext().getBean(IWechatPayIntceptor.class);
		return wechatPayIntceptor;
	}

	@Override
	public String getRoutePath() {
		
		return null;
	}
}
